const app = getApp();
const timeutil =require('../../utils/timeutil')
const db = wx.cloud.database();
Page({
  data: {
    bbbug: false,
    songList: [],
    room_id: 0,
  },
  onLoad(options) {
    if (!options.bbbug || options.bbbug != app.globalData.systemVersion) {
      return;
    }
    this.setData({
      bbbug: true
    });
    wx.setNavigationBarTitle({
      title: '搜索点歌',
    });
  },
  doSearchSong(e) {
    let that = this;
    wx.request({
      url: "https://api.bbbug.com/api/song/search",
      loading: "搜索中",
      data: {
        keyword: e.detail.value
      },
      success: function (res) {
        console.log(res)
        that.setData({
          songList: res.data.data
        });
      }
    });
  },
  showMenu(e) {
    let song = e.mark.item;
    let menu = ["点歌"];
    wx.showActionSheet({
      itemList: menu,
      success(res) {
        switch (menu[res.tapIndex]) {
          case '点歌':
            //写入云空间
            let userInfo = wx.getStorageSync('userInfo') || null
            wx.showLoading({
              title: '点歌中',
            })
            song.date = timeutil.TimeCode();
            song.nickname = userInfo.nickName
            song.playlength = 0
            song.playstatus = 0
            db.collection('MUSIC_LOG').add({
              data:song,
              success:res=>{
                wx.showToast({
                  title: '点歌成功',
                  icon:'success'
                })
                wx.hideLoading();
                setTimeout(function(){
                  wx.navigateBack({
                    delta: 0,
                  })
                },1000)
                //写入云端一个记录
                
              },
              fail:res=>{
                console.log(res)
              }
            })
            break;
          case '收藏':
            app.request({
              url: "song/addMySong",
              data: {
                room_id: app.globalData.roomInfo.room_id,
                mid: song.mid,
              },
              loading: "收藏中",
              success: function (res) {
                wx.showToast({
                  title: "收藏成功"
                });
              }
            });
            break;
          case '播放':
            app.request({
              url: 'song/playSong',
              data: {
                mid: song.mid,
                room_id: app.globalData.roomInfo.room_id
              },
              loading: "播放中",
              success: function (res) {
                wx.showToast({
                  title: '播放成功'
                });
              }
            });
            break;
          default:
        }
      }
    })
  }
})